<?php


namespace App\Api\Controller;

use App\Service\WeApp;
use App\Web\Model\RabbitMQ\ResumeUpdate;
use EchoPHP\Http\Request;
use App\Web\Model\EhireUser as EhireUser;


class Api extends BaseController {

    /**
     * @param Request $request
     */
    public function spyresumeupdate(Request $request) {
        $token = $request->getParam('token');
        $key = config('app.jycryptkey');
        if ($key != $token) {
            return $this->json(['status' => false, 'message' => '无权访问']);
        }
        $resumeIdString = $request->getParam('resumeid');
        $resumeIdString = str_replace(',', ';', $resumeIdString);
        $resumeIds = explode(';', $resumeIdString);
        $resumeIds = array_filter($resumeIds);
        if (empty($resumeIds)) {
            return $this->json(['status' => false, 'message' => '参数异常']);
        }
        $resumeUpdate = new ResumeUpdate();
        //获取在RPO系统已经使用的简历
        $resumeInRpoIds = $resumeUpdate->getResumeInRpo($resumeIds);

        if ($resumeInRpoIds['status'] == false) {
            return $this->json(['status' => false, 'message' => $resumeInRpoIds['message']]);
        }
        if (empty($resumeInRpoIds['data'])) {
            return $this->json(['status' => true, 'message' => '没有需要更新的简历']);
        }
        $resumeIds = explode(';', $resumeInRpoIds['data']);
        $addResult = $resumeUpdate->addToQueue($resumeIds);

        if (count($addResult['resultNack']) > 0) {
            return $this->json(['status' => false, 'message' => '保存失败']);
        }
        return $this->json(['status' => true, 'message' => '保存成功']);
    }

    /**
     * 网才调用新增用户回调接口
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function addsuperusercallback(Request $request) {
        $postData = [];
        $postData['ctmid'] = $request->getPost('ctmid');
        $postData['membername'] = $request->getPost('membername');
        $postData['hruid'] = $request->getPost('hruid');
        $postkey = $request->getPost('key');

        $result = ['code' => '00000', 'msg' => '保存成功', 'data' => []];
        if (empty($postData['ctmid']) || empty($postData['membername']) || empty($postData['hruid']) || empty($postkey)) {
            $result['code'] = '10001';
            $result['msg'] = '参数异常';
            return $this->json($result);
        }

        //参数键名正序排序
        ksort($postData);

        //按参数名升序排列的参数值字符串连接
        $allValue = '';
        foreach ($postData as $key => $item) {
            $allValue .= $item;
        }

        $options = config('extraapi.ehireapi', []);

        //生成参数校验码 md5(md5(参数值字符串连接（不包括key本身）+’EhireJava’))
        $key = md5(md5($allValue . $options['md5key']));
        $postData['membername'] = urldecode($postData['membername']);
        if ($key != $postkey) {
            $result['code'] = '10002';
            $result['msg'] = '签名错误';
            return $this->json($result);
        }

        $ehireUser = new EhireUser();
        $addResult = $ehireUser->addSuperUser($postData['ctmid'], $postData['hruid'], 1, 0);
        if (!$addResult['status']) {
            $result['code'] = '10003';
            $result['msg'] = $addResult['message'];
            return $this->json($result);
        }

        $result['msg'] = $addResult['message'];
        return $this->json($result);
    }
}